/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.cicp.service;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.util.DBConnectionUtil;
import com.sinodata.bsm.common.vo.ProtocolParameter;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author dongyushi
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-8-9 下午8:32:05          dongyushi        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class DBConnectionService {

    private static final Logger logger = Logger.getLogger(DBConnectionService.class);

    public Boolean testConn(ProtocolParameter protocolParameter) {

        String ip = protocolParameter.getIp();
        String dbName = protocolParameter.getInstName();
        String db = protocolParameter.getInstType();
        String url = protocolParameter.getUrl();
        Integer port = protocolParameter.getPort();
        String usr = protocolParameter.getUserName();
        String pwd = protocolParameter.getPwd();
        DBConnectionUtil dBConnectionUtil = DBConnectionUtil.getInstance();
        url = dBConnectionUtil.getDbUrl(url, ip, dbName, String.valueOf(port), usr, pwd);

        BasicDataSource bds = null;
        try {
            bds = dBConnectionUtil.initDataSource(url, usr, pwd, db);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }

        Connection conn = null;
        if (bds != null) {
            try {
                conn = bds.getConnection();
            } catch (SQLException e) {
                logger.error(e.getMessage());
            }
        }
        if (conn != null) {
            dBConnectionUtil.returnConnection(conn);
            return true;
        } else {
            return false;
        }
    }

}
